7

要实现的功能是根据用户购买过得商品进行推荐(猜你喜欢)
数据库结构:标签表label,商品标签关联表goods_label,商品表goods,商品sku表goods_sku,订单表order,订单商品快照表order_goods
实现原理:根据购买过商品的标签,为用户推荐商品
活不多说,上代码:

$memberid=$member->checktoken($param['token']);
$orderlist = $order->getorder(array('member_id'=>$memberid));
$list = array();
foreach ($orderlist as $value1) {
    $ordergoods = $order->getordergoods($value1['ordernumber']);
    foreach ($ordergoods as $value) {
        $labellist = array();
        $label_id = $that->labegoodsinfo(array('goods_id'=>$value['goods_id']));
        $labellist = explode(',', $label_id);
        $list = array_merge($list,$labellist);
        $list = array_unique($list);
        //$list即为用户购买过的商品标签
       //格式:$list=array(0=>1,1=>3,2=>5,3=>4);
    }
}
$label_goods = $that->getgoodslabel();
    foreach ($label_goods as $k=>$value) {
        $goods_labelid = explode(',', $value['label_id']);
        $label_goods[$k]['count'] = count(array_intersect($goods_labelid,$list));
        if($label_goods[$k]['count'] == 0){
            unset($label_goods[$k]);
    }
}
    //$label_goods是符合$list标签的商品以及count是符合的标签个数
    //格式:$label_goods=array(0=>array('label_id'=>1,3,6,'goodsid'=>1,'count'=>2),
    //                        1=>array('label_id'=>1,3,5,'goodsid'=>2,'count'=>3)
    //                        )
$sort = array(
    'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
    'field'     => 'count',       //排序字段
);
$arrSort = array();
foreach($label_goods AS $uniqid => $row){
    foreach($row AS $key=>$value){
        $arrSort[$key][$uniqid] = $value;
    }
}
if($sort['direction']){            
array_multisort($arrSort[$sort['field']],constant($sort['direction']),$label_goods);
}
//$label_goods重新排序,按照符合标签数由多到少排序
//格式:$label_goods=array(0=>array('label_id'=>1,3,5,'goodsid'=>2,'count'=>3),
//                        1=>array('label_id'=>1,3,6,'goodsid'=>1,'count'=>2)
//                        )

最后只要根据goodsid去查询出相关商品的信息即可。
没有用到权重等相关算法,欢迎大家多提意见,让我更好的进步!
爱你哟~


Alisa
147 声望7 粉丝

把自己当做一件产品去经营,货真价实